package com.hspedu.newData.dmsxl.字符串;

import org.junit.Test;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-05-07 
 * @Version: 1.0
 */

public class LC415字符串相加 {

    public String addStrings(String num1, String num2) {

        int cnt = 0;
        StringBuilder sb = new StringBuilder();
        for (int i = num1.length() - 1,j = num2.length() - 1; i >= 0 || j >= 0; i--,j--) {

            int a = 0;
            int b = 0;
            if (i>=0){
                a = num1.charAt(i) - '0';
            }
            if (j>=0){
                b = num2.charAt(j) - '0';
            }

            int sumX = (a + b + cnt);
            int sum = (a + b + cnt) %10;
            cnt = sumX / 10;
            sb.append(sum);
        }

        if (cnt != 0){
            sb.append(cnt);
        }

        String string = sb.reverse().toString();
        return string;
    }

    @Test
    public void test() {
        // 测试代码

        String s = addStrings("11", "123");
        String s1 = addStrings("511", "823");
        String s2 = addStrings("0", "0");
        System.out.println("=---");
    }
}
